<?php
namespace lib\http;
class HttpCode
{
    private $jpeg;
    public function __construct($parameter)
    {
        $img_url= WEB_ROOT .'/static/vi/' .rand(1,8).'.jpg';
        $this->jpeg=imagecreatefromjpeg($img_url);
        $code=$this->code();
        $_SESSION['HttpCode']=strtolower($code);
    }
    private function code()//随机的验证码
    {
        $str="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789";
        $str=str_shuffle($str);
        $code=substr($str,0,4);
        for ($i=0;$i<strlen($code);$i++)
        {
            $font_color=imagecolorallocate($this->jpeg,rand(0,255),rand(0,255),rand(0,255));
            $size=rand(25,30);
            imagettftext($this->jpeg,$size,rand(-90,90),rand(40*($i+1)-5,40*($i+1)),rand(30,50),$font_color, WEB_ROOT .'/static/1.ttf',$code{$i});
        }
        return $code;
    }
    private function line()//画干扰线
    {

    }
    public function output()//输出图片
    {
        header('Content-Type: image/jpeg');
        imagejpeg($this->jpeg,WEB_ROOT .'/111.jpg');
        return '111';
    }
    public function __destruct()
    {
        imagedestroy($this->jpeg);//销毁一图像
        // TODO: Implement __destruct() method.
    }
    public static function verify($code)
    {
        if (!$_SESSION['HttpCode'])return false;
        $bool=$_SESSION['HttpCode']==strtolower($code)?true:false;
        return $_SESSION['HttpCode'];
        unset($_SESSION['HttpCode']);
        return $bool;
    }
}
/*
namespace lib\http;
class HttpCode
{
    private $jpeg;
    public function __construct()
    {
        //开始时随机选择一张图片作为背景图
        $img_url= WEB_ROOT . '/static/vi/' .rand(1,8).'.jpg';
        $this->jpeg=imagecreatefromjpeg($img_url);
        //$this->code();
        $code=$this->code();
        session_start();
        $_SESSION['HttpCode']=strtolower($code);//将大写转为小写
        $this->line();
    }

    private function code()   //随机的验证码
    {
        $str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';//验证码
        $str=str_shuffle($str);//随机打乱验证码
        $code=substr($str,0,4);//取前四位
        for ($i=0;$i<strlen($code);$i++)
        {
            $color=imagecolorallocate($this->jpeg,rand(0,120),rand(0,120),rand(0,120));//设置随机字体颜色
            $size=rand(25,30);
            imagettftext($this->jpeg,$size,rand(-90,90),rand(40*($i+1)-5,40*($i+1)),rand(30,50), $color, WEB_ROOT  . '/static/typeface/' .rand(1,6).'.ttf',$code{$i});
        }
        return $code;
    }
    private function line()  //画干扰线
    {
        $color=imagecolorallocate($this->jpeg,rand(0,120),rand(0,120),rand(0,120));//设置随机字体颜色
        imageline($this->jpeg,rand(20,50),rand(20,40),rand(130,180),rand(21,70),$color);//画一条线段
        imageline($this->jpeg,rand(20,50),rand(40,60),rand(130,180),rand(41,70),$color);//画一条线段
        imageline($this->jpeg,rand(20,50),rand(20,60),rand(130,180),rand(21,70),$color);//画一条线段
    }
    public function output()    //输出图片
    {
        header('Content-Type: image/jpeg');
        imagejpeg($this->jpeg);//以 PNG 格式将图像输出到浏览器或文
    }
    public function __destruct()
    {
        //使用完毕就将图片资源销毁掉
        imagedestroy($this->jpeg);
    }
    public static function varify($code)    //验证
    {
        if (!$_SESSION['HttpCode'])return false;
        $bool=$_SESSION['HttpCode']==strtolower($code)?true:false;//strtolower()将大写转为小写
        unset($_SESSION['HttpCode']);
        return $bool;
    }
}
*/